package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.LoginInfo;
import com.woniuxy.java106crm.entity.Product;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.LoginInfoService;
import com.woniuxy.java106crm.util.RandomUtils;
import com.woniuxy.java106crm.vo.FindLoginInfoVo;
import com.woniuxy.java106crm.vo.FindProductVo;
import com.woniuxy.java106crm.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 登录日志管理
 */
@Api(tags = "登录日志管理API文档")
@Slf4j
@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {
    @Resource
    private LoginInfoService loginInfoService;

    //根据姓名查询产品信息
    @RequireMethod("查询登录日志")
    @PostMapping("/findLoginInfoByUname")
    public ResponseResult<ResPage<List<LoginInfo>>> findLoginInfoByUname(@RequestBody FindLoginInfoVo findLoginInfoVo){
//        log.debug(findLoginInfoVo.toString());
        ResPage<List<LoginInfo>> info = loginInfoService.findLoginInfoByUname(findLoginInfoVo);
        return new ResponseResult<>(ResponseResult.OK,info);
    }

    //删除登录信息
    @RequireMethod("删除登录信息")
    @DeleteMapping("/delLoginInfo/{id}")
    public ResponseResult<Boolean> delLoginInfo(@PathVariable("id") Integer id){
//        log.debug("传过来的id值为：{}",id);
        return new ResponseResult<>(ResponseResult.OK,loginInfoService.delLoginInfo(id));
    }

    //批量删除
    @RequireMethod("批量删除登录信息")
    @DeleteMapping("/putchDel/{ids}")
    public ResponseResult<Boolean> putchDel(@PathVariable("ids") String ids){
//        log.debug("传过来的id值为：{}",ids);
        return new ResponseResult<>(ResponseResult.OK,loginInfoService.putchDel(ids));
    }

    //根据登录用户查询用户信息
    @RequireMethod("根据用户id查询用户信息")
    @GetMapping("/queryUserInfoByUid")
    public ResponseResult<UserVo> queryUserInfoByUid(HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        log.debug(uid.toString());
        return new ResponseResult<>(ResponseResult.OK,loginInfoService.queryUserInfoByUname(uid));
    }
    //修改用户信息
    @RequireMethod("修改用户信息")
    @PostMapping("/editUserInfo")
    public ResponseResult<Boolean> editUserInfo(@RequestBody UserVo userVo,HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        userVo.setId(uid);
//        log.debug(userVo.toString());
//        return null;
        return new ResponseResult<>(ResponseResult.OK,loginInfoService.editUserInfo(userVo));
    }
    /**
     * 实现上传图片的操作
     */
    @RequireMethod("修改用户头像")
    @PostMapping("updateImage")
    public ResponseResult<String> updateImage(MultipartFile file, UserVo userVo,HttpSession session) throws IOException {
        //定义基础路径
//        String url = "http://localhost:80/";
        //1.定义父目录
        String basePath = "E://img";
        //2.获取文件的原名称
        String filename = file.getOriginalFilename();//获取上传的文件的原名称
        //3.截取文件的后缀名(从后往前搜索)
        int start = filename.lastIndexOf(".");
        //从“.”的位置开始截取
        String suffix = filename.substring(start);
        //4.随机生成一个新的文件名称
        String prefix = RandomUtils.createRandomStringUseTime("TX");
        //5.把新的文件名和后缀进行组合
//        String newFile = prefix + suffix;
        String image = prefix + suffix;
        File f = new File(basePath,image);
        // 6.把文件写入到指定的目录下
        file.transferTo(f);
        //设置车辆对象的路径
//        String image = url + newFile;
        //添加图片的路径到对象中
        Integer uid = (Integer) session.getAttribute("uid");
        userVo.setId(uid);
        userVo.setImage(image);
        //调用service层方法
        loginInfoService.updateImage(userVo);
        return new ResponseResult<>(ResponseResult.OK,image);
    }


}
